package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Employee;
import cn.wolfcode.car.business.mapper.EmployeeMapper;
import cn.wolfcode.car.business.query.EmployeeQuery;
import cn.wolfcode.car.business.service.DepartmentService;
import cn.wolfcode.car.business.service.EmployeeService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author WaveHi
 * @Date 19/11/2021 下午 2:29
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

//    @Autowired
//    private EmployeeMapper employeeMapper;

    @Autowired
    private DepartmentService departmentService;

    @Override
    public TablePageInfo<Employee> getPartOfEmployees(EmployeeQuery employeeQuery) {
        QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>();
        employeeQueryWrapper
                .like(employeeQuery.getKey() != null,
                        "name",
                        employeeQuery.getKey())
                .or()
                .like(employeeQuery.getKey() != null,
                        "email",
                        employeeQuery.getKey())
                .eq(employeeQuery.getAdmin() != null,
                        "admin",
                        employeeQuery.getAdmin())
                .eq(employeeQuery.getStatus() != null,
                        "status",
                        employeeQuery.getStatus())
                .eq(employeeQuery.getDeptId() != null,
                        "dept_id",
                        employeeQuery.getDeptId())
                .ge(employeeQuery.getStartTime() != null,
                        "hiredate", employeeQuery.getStartTime())
                .le(employeeQuery.getEndTime() != null,
                        "hiredate", employeeQuery.getEndTime());
        List<Employee> list =
                list(employeeQueryWrapper);
        for (Employee employee : list) {
            employee.setDepartment(departmentService.getDepartment(employee.getDeptId()));
        }
        return new TablePageInfo<>(list);
//        return new TablePageInfo<>(employeeMapper.selectByCondition(employeeQuery));
    }

    @Override
    public Employee getEmployee(Long id) {
        Employee byId = getById(id);
        byId.setDepartment(departmentService.getDepartment(byId.getDeptId()));
        return byId;
//        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(Employee employee) {
        save(employee);
//        employeeMapper.insert(employee);
    }

    @Override
    public void update(Employee employee) {
        updateById(employee);
//        employeeMapper.updateByPrimaryKey(employee);
    }

    @Override
    @Transactional
    public void remove(Long[] ids) {
        for (Long id : ids) {
            removeById(id);
        }
//        for (Long id : ids) {
//            employeeMapper.deleteByPrimaryKey(id);
//        }
    }
}
